<#--  添加答案 集合 -->
<@function>
	<#-- 已登录 -->
	<#if systemUser != null>
		<#--  返回Map<String,Object>类型 -->
		<#assign returnValue = questionRelated_addAnswer_collection>
		
		<#--  全局允许提交答案 -->
		<#if returnValue['allowAnswer'] == true>
			<div class="answer-formModule">
				<div class="addAnswer-wrap">
					<form id="addAnswerForm" action="user/control/answer/add?questionId=${url_questionId}&page=${url_page}" method="post">
						
						<#-- 提交后跳转参数  参数必须经过encodeBase64URL编码后才能提交 不填写时默认跳转回当前页 -->
						<input type="hidden" id="jumpUrl" name="jumpUrl" value="">
						<p><textarea class="textarea" onfocus="" id="answerContent" name="content" >${answer.content}</textarea></p>
						
						<span id="${identificationNumber}_content_error" class="error">${(error['content'])!}</span> 
						<span id="${identificationNumber}_answer_error" class="error">${error['answer']}</span>
						<div class="form-action">
							<#-- 获取令牌 -->
							<input type="hidden" id="token" name="token" value="${token!}">
							<#-- 获取评论支持标签 -->
							<input type="hidden" id="availableTag" name="availableTag" value="${(returnValue['availableTag'])!}">
							<#-- 文件系统 -->
							<input type="hidden" id="fileSystem"  value="${(returnValue['fileSystem'])!}">
							
							<div class="answerSubmit">
					        	<!-- <span class="submitButton"><INPUT type="button" value="提交" onClick="javascript:sureSubmit(this.form)"></span> -->
					        	<span class="submitButton"><INPUT type="button" id="${identificationNumber}_submitForm" value="提交" onClick="javascript:ajax_sureSubmit('${identificationNumber}')"></span>
					        </div>
							<#-- identificationNumber:识别号：用来区别每次请求 -->
							<#-- 验证码 -->
							<#if returnValue['captchaKey'] != "">	
							<div class="captchaModule">
								<div class="captcha_div">
									<div class="captchaBox">
										<input type="hidden" id="${identificationNumber}_captchaKey" name="captchaKey" value="${(returnValue['captchaKey'])!}">
										<input id="${identificationNumber}_captchaValue" name="captchaValue" type="text" maxLength="5" autocomplete="off" tabindex="3" onfocus="inputBoxTip_click(this);" onblur="inputBoxTip_leave('${identificationNumber}',this);"/>
										<label class="promptTag" id="${identificationNumber}_captchaValueBoxTip"  for="captchaValue">验证码</label>
									</div>
									<a class="captchaImage" href="javascript:void(0);" >
										<img id="${identificationNumber}_captcha" src="captcha/${(returnValue['captchaKey'])!}.jpg" onClick="replaceCaptcha('${identificationNumber}');" tabindex="4" />
									</a>
									<a  href="javascript:void(0);" onClick="replaceCaptcha('${identificationNumber}');" tabindex="5">换一幅</a>
								</div>
								<p class="tips">
									
									<#-- 验证码错误 -->
									<span id="${identificationNumber}_captchaValue_error">${(error['captchaValue'])!}</span>
									
								</p>
							</div>
							</#if>
							
							
						</div>
						<div class="promptInfo">
							<h2>我来说两句，分享我的答案</h2>
						</div>
						<script language="javascript" type="text/javascript">
							//点击输入框提示 
							function inputBoxTip_click(obj){
								document.getElementById(obj.id+"BoxTip").style.display="none";
							
							}
							//离开输入框提示
							function inputBoxTip_leave(identificationNumber,obj){
								if(obj.value != ""){
									document.getElementById(obj.id+"BoxTip").style.display="none";
								}else{
									document.getElementById(obj.id+"BoxTip").style.display="";
								}
								verificationCode(identificationNumber);
							}
							
							//校对验证码 identificationNumber:识别号：用来区别每次请求
							function verificationCode(identificationNumber){
								var flag = true;
								if( document.getElementById(identificationNumber+"_"+"captchaKey") != null){
									
									var captchaKey = document.getElementById(identificationNumber+"_"+"captchaKey").value;
									var parameter = document.getElementById(identificationNumber+"_"+"captchaValue").value;
									if(parameter != ""){
									
										var parameter_trim = trim(parameter);
										if(parameter_trim != ""){
											//ajax使用同步提交
											get_request(function(value){
								            	if(value == "false"){
								            	
									            	document.getElementById(identificationNumber+"_"+"captchaValue_error").innerHTML = "验证码错误";
									            	flag = false;
								            	}
								            },
										 		"checkCaptcha?captchaKey="+captchaKey+"&captchaValue="+parameter_trim+"&timestamp=" + new Date().getTime(), false);
								            	
										}else{
											document.getElementById(identificationNumber+"_"+"captchaValue_error").innerHTML = "验证码不能为空";
											flag = false;
										}
									}else{
										document.getElementById(identificationNumber+"_"+"captchaValue_error").innerHTML = "验证码不能为空";
										flag = false;
									}
									if(flag == true){
										document.getElementById(identificationNumber+"_"+"captchaValue_error").innerHTML = "";
									}
								}
							
								return flag;
							}
							//更换验证码
							function replaceCaptcha(identificationNumber){
								var captchaKey = document.getElementById(identificationNumber+"_"+"captchaKey").value;
								document.getElementById(identificationNumber+"_"+"captcha").src = "captcha/"+captchaKey+".jpg?" + Math.random(); 
							}
							//解决提交按钮的click和富文本的blur事件冲突
							$(document).ready(function(){
								$("input[type='button']").mousedown(function(e){
							    	e.preventDefault();
								});
							});
							//表单Form方式提交
							function sureSubmit(obj){
								obj.submit();
							} 
							//Ajax方式提交
							function ajax_sureSubmit(identificationNumber){
								//按钮设置 disabled="disabled"
								document.getElementById(identificationNumber+"_submitForm").disabled=true;
								if(verificationCode() == true){
									//清除错误信息
									document.getElementById(identificationNumber+"_content_error").innerHTML = "";
							    	document.getElementById(identificationNumber+"_answer_error").innerHTML = "";
								
									var parameter = "";
									parameter += "&questionId="+getUrlParam("questionId");//获取URL参数
	
									
									//获取令牌
									parameter += "&token="+document.getElementById("token").value;
									
									//获取验证码key
									var captchaKey = document.getElementById(identificationNumber+"_"+"captchaKey");
									if(captchaKey != null){
										parameter += "&captchaKey="+captchaKey.value;
									}
									
									//获取验证码value
									var captchaValue = document.getElementById(identificationNumber+"_"+"captchaValue");
									if(captchaValue != null){
										parameter += "&captchaValue="+captchaValue.value;
									}
									
									//获取内容
									var content = document.getElementById("answerContent").value;
									parameter += "&content="+encodeURIComponent(content);
								
								   	//删除第一个&号,防止因为多了&号而出现警告: Parameters: Invalid chunk ignored.信息
									if(parameter.indexOf("&") == 0){
										parameter = parameter.substring(1,parameter.length);
									}
								   	post_request(function(value){
										var data = JSON.parse(value);
										for(var returnValue in data){
											
											if(returnValue == "success"){
							        			if(data[returnValue] == "true"){
							        				//需引入layer
							        				layer.msg('提交成功,3秒后自动刷新本页', 
														{
														  time: 3000 //3秒关闭（如果不配置，默认是3秒）
														},function(){//关闭后的操作
															//刷新页面
							        						document.location.reload();
														}
													);
							        			}
							        		}else if(returnValue == "error"){
							        			
							        			var error = data[returnValue];
							        			for(var key in error){
							        				var error_html = document.getElementById(identificationNumber+"_"+key+"_error");
							        	
							        				if(error_html != null){
							        					error_html.innerHTML = error[key];
							        				}
							        				
							        			}
							        			//按钮设置 disabled="disabled"
												document.getElementById(identificationNumber+"_submitForm").disabled=false;
							        		}else if(returnValue == "captchaKey"){//显示验证码
					        					
												var captchaKey = data[returnValue];
												if(captchaKey != ""){
													//设置验证码Key
													if(document.getElementById(identificationNumber+"_"+"captchaKey")!= null){
					
														document.getElementById(identificationNumber+"_"+"captchaKey").value = captchaKey;
														//设置验证码图片
														replaceCaptcha(identificationNumber);
													}	
												}
											}
							        	}
									},
										"user/control/answer/add?timestamp=" + new Date().getTime(), true,parameter);
								}else{
									//按钮设置 disabled="disabled"
									document.getElementById(identificationNumber+"_submitForm").disabled=false;
								}
							}  
							 
							
							var availableTag = ['source', '|'];
							KindEditor.options.cssData = ""+
							".ke-content .prettyprint {"+
								"background:#f8f8f8;"+
								"border:1px solid #ddd;"+
								"padding:5px;"+
							"}"+//默认字体大小
							"body {"+
								"font-size: 14px;"+
							"}";
							var editor;
							KindEditor.ready(function(K) {
								var availableTag = document.getElementById("availableTag").value;
								var availableTag_obj = null;//['source', '|','fontname','fontsize','emoticons'];
								if(availableTag != ""){
									var availableTag_obj = JSON.parse(availableTag);//JSON转为对象
									for(var i=0; i< availableTag_obj.length; i++){
										var _availableTag = availableTag_obj[i];
										if(_availableTag == "image"){//增加批量插入图片按钮
											availableTag_obj.splice(i+1, 0, 'multiimage');//在指定索引处插入元素
											break;
										}
									}
								}
								//获取URL中的话题Id
								var questionId = getUrlParam("questionId");
								var fileSystem = document.getElementById("fileSystem").value;
								
								editor = K.create('textarea[id="answerContent"]', {
									themeType : 'style :minimalist',//极简主题 加冒号的是主题样式文件名称同时也是主题目录
									basePath : '${commonPath}js/kindeditor/',//指定编辑器的根目录路径
									formatUploadUrl :false,//false时不会自动格式化上传后的URL
									resizeType : 1,//2或1或0，2时可以拖动改变宽度和高度，1时只能改变高度，0时不能拖动。默认值: 2 
									autoHeightMode : true,//自适应高度
									allowPreviewEmoticons : true,//true或false，true时鼠标放在表情上可以预览表情
									allowImageUpload : true,//true时显示图片上传按钮
									uploadModule : parseInt(fileSystem),//上传模块 0.本地 10.SeaweedFS 20.MinIO 30.阿里云OSS
									uploadJson :'user/control/answer/uploadImage?questionId='+questionId,//指定浏览远程图片的服务器端程序
									filePostName:'file',//文件上传字段 默认imgFile
									fixToolBar : true,//浮动工具栏
									items : availableTag_obj,
									afterChange : function() {
										this.sync();
									},
									afterBlur:function(){
										this.sync();
									}
								});
							});
							
							</script>
						
					</form>
					
				</div>
			</div>
		<#else>
			<div class="addAnswerModule" >
				<div class="addAnswer-wrap">
					<div class="respond">
						<p>
							全站关闭回答
						</p>
					</div>
				</div>
			</div>
		</#if>
		
	<#else>
		<#-- 未登录 -->
		<div class="addAnswerModule" >
			<div class="addAnswer-wrap">
				<div class="respond">
					<p >请 
					<a href="login">登录</a> 
						后回答问题。没有帐号？
						<a href="register">注册</a> 一个。
					</p>
				</div>
			</div>
		</div>
	</#if>
</@function>



